package com.maple.controller.user;


import com.alipay.api.AlipayApiException;
import com.maple.dto.OrdersDTO;
import com.maple.dto.OrdersPageQueryDTO;
import com.maple.dto.OrdersSubmitDTO;
import com.maple.result.PageResult;
import com.maple.result.Result;
import com.maple.service.OrderService;
import com.maple.utils.AlipayUtils;
import com.maple.utils.ThreadLocalUtils;
import com.maple.vo.OrderSubmitVO;
import com.maple.vo.OrderVO;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
public class OrderController {

    @Autowired
    OrderService orderService;


    @PostMapping("/submit")
    public Result submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.sudmit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    @GetMapping("/historyOrders")
    public Result<PageResult> history(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("历史订单查询：{}", ordersPageQueryDTO);
        ordersPageQueryDTO.setUserId(ThreadLocalUtils.getCurrentId());
        PageResult list = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(list);
    }

    @GetMapping("/orderDetail/{id}")
    public Result orderDetail(@PathVariable String id){
        log.info("查询订单详情");
        OrderVO orderVO = orderService.orderDetail(id);
        return Result.success(orderVO);
    }


    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable String id) throws AlipayApiException {
        log.info("取消订单：{}", id);
        orderService.userCancel(id);
        return Result.success();
    }


    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable String id){
        log.info("再来一单：{}", id);
        orderService.repetition(id);
        return Result.success();
    }


    @GetMapping("/remider/{id}")
    public Result reminder(@PathVariable String id){
        log.info("催单：{}", id);
        orderService.reminder(id);
        return Result.success();
    }

}
